(function(){

	var v = "1.3.2";

  var myCss = document.createElement("link");
  myCss.setAttribute("rel", "stylesheet");
  myCss.setAttribute("type", "text/css");
  myCss.setAttribute("href", "//fbo-bookmarklet.googlecode.com/git/css/main.css");		
  document.getElementsByTagName("head")[0].appendChild(myCss);
  
  //var myCss = document.createElement("link");
  //myCss.setAttribute("rel", "stylesheet");
  //myCss.setAttribute("type", "text/css");
  //myCss.setAttribute("href", "//fbo-bookmarklet.googlecode.com/git/css/bootstrap.css");		
  //document.getElementsByTagName("head")[0].appendChild(myCss);

  //var script = document.createElement("script");
  //script.src = "//fbo-bookmarklet.googlecode.com/git/scripts/lib/bootstrap.js";  
  //document.getElementsByTagName("head")[0].appendChild(script);

	if (window.jQuery === undefined || window.jQuery.fn.jquery < v) {
		var done = false;
		var script = document.createElement("script");
		script.src = "//ajax.googleapis.com/ajax/libs/jquery/" + v + "/jquery.min.js";
		script.onload = script.onreadystatechange = function(){
			if (!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {
				done = true;
				initMyBookmarklet();
			}
		};
		document.getElementsByTagName("head")[0].appendChild(script);
		
		
		
	} else {
		initMyBookmarklet();
	}
	
	function initMyBookmarklet() {
		(window.myBookmarklet = function() {
			if ($("#my-fbo-main") == null || $("#my-fbo-main").length == 0) {
          $("body").append("\
					<div id='my-fbo-main'>\
            <div id='close'>Close</div>\
            <div id=my-agency-logo></div/>\
            <h1 id=my-agency-title></h1/>\
            <div id=my-solicitation-number></div/>\
            <div id=my-agency-name></div/>\
            <div id=my-sidebar-related-notices></div/>\
            <div id=my-notice-type></div/>\
            <div id=my-posted-date></div/>\
            <div id=my-response-date></div/>\
            <div id=my-set-aside></div/>\
            <div id=my-classification-code></div/>\
            <div id=my-naics-code></div/>\
					</div>");
			} else {
				$("#my-fbo-main").fadeIn(500);
				setTimeout("$('#my-fbo-main').remove()", 750);
			}
			
			$('#close').click(function(){
        setTimeout("$('#my-fbo-main').remove()", 250);
      });
		})();
		
		populateData = function(my_selector, fob_selector, prefix_text) {
      if($(fob_selector) != null && $(fob_selector).length > 0) {
        $(my_selector).text(prefix_text + $(fob_selector).eq(0).text());
      } 
      else {
        $(my_selector).text(prefix_text + 'N/A');
      }
		}
		
		$('#my-agency-logo').html($('.agency-logo').html());
		$('#my-agency-title').text($('.agency-header h2').text());
		$('#my-agency-name').html($('.agency-name').html());
		
		
		$('#my-sidebar-related-notices').text("Number of change notices: " + $('ul#sb_related_notices li:contains("Changed")').length);
		
		populateData('#my-notice-type', '#dnf_class_values_procurement_notice__procurement_type__widget', 'Notice Type: ');
		populateData('#my-posted-date', '#dnf_class_values_procurement_notice__posted_date__widget', 'Posted Date: ');
		populateData('#my-response-date', '#dnf_class_values_procurement_notice__response_deadline__widget', 'Response Date: ');
		populateData('#my-set-aside', '#dnf_class_values_procurement_notice__original_set_aside__widget', 'Number of change notices: ');
		populateData('#my-classification-code', '#dnf_class_values_procurement_notice__classification_code__widget', 'Classification Code: ');
		populateData('#my-naics-code', '#dnf_class_values_procurement_notice__naics_code__widget', 'NAICS Code: ');
	}

})();